﻿using AllInOne.Common;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AllInOne
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    [AddINotifyPropertyChangedInterface]
    public partial class MainView : Window
    {
        #region 要设置的参数
        /// <summary>
        /// WebApi接口地址
        /// </summary>
        public string ApiUrl { get; set; }

        /// <summary>
        /// 主窗体标题
        /// </summary>
        public string MainTitle { get; set; }

        /// <summary>
        /// Socket服务端IP
        /// </summary>
        public string SocketServerIP { get; set; }

        /// <summary>
        /// Socket服务端端口
        /// </summary>
        public int SocketServerPort { get; set; }

        /// <summary>
        /// 汽车衡一编号
        /// </summary>
        public string ScaleCode1 { get; set; }

        /// <summary>
        /// 汽车衡一名称
        /// </summary>
        public string ScaleFlag1 { get; set; }

        /// <summary>
        /// 汽车衡二编号
        /// </summary>
        public string ScaleCode2 { get; set; }

        /// <summary>
        /// 汽车衡二名称
        /// </summary>
        public string ScaleFlag2 { get; set; }

        /// <summary>
        /// 列表显示条数
        /// </summary>
        public int ShowSize { get; set; }

        /// <summary>
        /// 语音提示语
        /// </summary>
        public string Voice { get; set; }

        /// <summary>
        /// 上传园区系统地址
        /// </summary>
        public string UploadUrl { get; set; } = "";
        #endregion


        public MainView()
        {
            InitializeComponent();
        }

        private void OkClick(object sender, RoutedEventArgs e)
        {
            //保存
            INIHelper.Write("ALL", "ApiUrl", ApiUrl);
            INIHelper.Write("ALL", "MainTitle", MainTitle);
            INIHelper.Write("ALL", "SocketServerIP", SocketServerIP);
            INIHelper.Write("ALL", "SocketServerPort", TextBox_SocketServerPort.Text);
            INIHelper.Write("ALL", "ScaleCode1", ScaleCode1);
            INIHelper.Write("ALL", "ScaleFlag1", ScaleFlag1);
            INIHelper.Write("ALL", "ScaleCode2", ScaleCode2);
            INIHelper.Write("ALL", "ScaleFlag2", ScaleFlag2);
            INIHelper.Write("ALL", "ShowSize", TextBox_ShowSize.Text);
            INIHelper.Write("ALL", "Voice", Voice);
            INIHelper.Write("ALL", "UploadUrl", UploadUrl);
            MessageBox.Show("保存成功,但需要重新启动程序才能起作用！", "提示");
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //读取参数据
            ApiUrl = INIHelper.Read("ALL", "ApiUrl");
            MainTitle = INIHelper.Read("ALL", "MainTitle");
            SocketServerIP = INIHelper.Read("ALL", "SocketServerIP");
            SocketServerPort = Convert.ToInt32(INIHelper.Read("ALL", "SocketServerPort"));
            ScaleCode1 = INIHelper.Read("ALL", "ScaleFlag1");
            ScaleFlag1 = INIHelper.Read("ALL", "ScaleName2");
            ScaleCode2 = INIHelper.Read("ALL", "ScaleFlag2");
            ScaleFlag2 = INIHelper.Read("ALL", "ScaleName2");
            ShowSize = Convert.ToInt32(INIHelper.Read("ALL", "ShowSize"));
            Voice = INIHelper.Read("ALL", "Voice");
            UploadUrl = INIHelper.Read("ALL", "UploadUrl");
        }

        private void TextBox_SocketServerPort_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            /*
             注释一下，正则表达式[\u4e00-\u9fa5]表示匹配中文，则正则表达式[^\u4e00-\u9fa5]匹配非中文。用Replace方法将匹配的非中文替换为空字符串，也就是去除了文本中的非中文字符。
            string _s= Regex.Replace(_s, @"[\u4e00-\u9fa5]", ""); //去除汉字
            string _s= Regex.Replace(_s, @"[^\u4e00-\u9fa5]", ""); //只留汉字
            string ph = Regex.Replace(ph, @"\D", ""); //排除除数字外的所有字符

            //禁止非数字输入
            e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
             */

            //排除除数字外的所有字符
            TextBox_SocketServerPort.Text = Regex.Replace(TextBox_SocketServerPort.Text, @"\D", "");
        }
        private void TextBox_ShowSize_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            //排除除数字外的所有字符
            TextBox_ShowSize.Text = Regex.Replace(TextBox_SocketServerPort.Text, @"\D", "");
        }


        private void TextBox_IP_LostFocus(object sender, RoutedEventArgs e)
        {
            //IP格式判断
            string ip = TextBox_IP.Text;
            if (!Regex.IsMatch(ip, "((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))"))
            {
                MessageBox.Show("IP格式不对，请重新输入！");
                //设置默认格式
                TextBox_IP.Text = "127.0.0.1";
                //重新设置焦点
                TextBox_IP.Focusable = true;
                Keyboard.Focus(TextBox_IP);
            }
        }
    }
}
